// pages/tips/tips.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var ans = new Array();  //答案数组初始化，会在onload函数中赋初值""
var nums = new Array();
var dan = {}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dates: util.formatTime(new Date()),
    num: nums,
    ii:0,
    id: 0,
    tag: 0,
    big: 1,
    //保存输入的选项信息
    danArr : dan,
    tags: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
    tagAnc: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'i', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
    arrays: ['点击选择问题类型', '单选', '多选', '获取日期', '单行文本输入', '文本输入', '获取地址'],
    index: 0,
    indes: 0,
    indis : [-1, 1, 2, 3, 4, 5, 6],
    next:false,
    ifC : false
  },
  bindPickerChange: function (e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    if(e.detail.value==0){
      this.ifC = false
    }else{
      this.ifC = true
    }
    nums = new Array()
    nums[0] = 1
    this.big = 1,
    this.data.ii=0;
    this.data.num = nums
    this.tag = e.detail.value
    this.data.danArr = {}
    this.setData({
      index: e.detail.value,
      tag: e.detail.value,
      nums : nums,
      big : 1,
      ii : 0,
      num : nums
    })
  },
  //提交
  submit: function (e) {
    if (!this.ifC){
      wx.showToast({
        title: '请选择题目类型',
        icon: 'none',
        duration: 2000
      })
    }else{
      var that = this;
      var answer_name = e.detail.value.answer_name; 
      var answer_picker = e.detail.value.answer_picker;
      answer_picker = parseInt(answer_picker);
      var answer_ifMust = e.detail.value.answer_ifMust;
      if(answer_ifMust.length==0){
        answer_ifMust = 0;
      }else{
        answer_ifMust = 1;
      }
      var keys = Object.keys(e.detail.value)
      var values = Object.values(e.detail.value)
      var body = {}
      //保存选项信息
      var ans = []
      //单选和多选要添加选项
      if(answer_picker=="1"||answer_picker=="2"){
        for (var i = 0; i < keys.length; i++) {
          if (keys[i] + "" == "answer_name") {
          } else if (keys[i] + "" == "answer_picker") {
          } else if (keys[i] + "" == "answer_ifMust") {
          } else {
            ans.push(values[i])
          }
        }
      }
      body["option"] = ans
      body["answer_name"] = answer_name
      body["answer_picker"] = answer_picker
      body["answer_ifMust"] = answer_ifMust
      console.log(body)
      this.addQ_put(body)
    }
    
  },
  //提交题目
  addQ_put:function(body){
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: api.Add_Question_URL,
      method: 'POST',
      dataType: 'json',
      data: {
        option: body["option"],
        answer_name: body["answer_name"],
        answer_picker: body["answer_picker"],
        answer_ifMust: body["answer_ifMust"],
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        wx.hideLoading()
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      },
      fail: function (res) {
        wx.showToast({
          title: '提交失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  //将单选与多选选项信息保存
  inputDan:function(e){
    var id = e.currentTarget.dataset.value
    var t = e.detail.value
    this.data.danArr[id] = t
    console.log(this.data.danArr)
  },
  inputDuo: function (e) {
    var id = e.currentTarget.dataset.value
    var t = e.detail.value
    this.data.danArr[id] = t
    console.log(this.data.danArr)
  },
  //删除选项
  delate:function(e){
    var id = e.currentTarget.dataset.value
    var keys = Object.keys(this.data.danArr)
    var values = Object.values(this.data.danArr)
    var danws = {}
    var k = 1
    for (var i = 0; i < keys.length; i++) {
      if(keys[i] != id){
        danws[k] = values[i]
        k++
      }
    }
    this.data.danArr = danws
    var arr = new Array()
    for (var i = 0; i < this.data.big-1; i++) {
      
        arr.push(i+1);
      
    }
    if (this.data.big == 1){
      arr.push(1);
    }
    this.data.num = arr
    console.log(id, this.data.num, this.data.danArr)
    if (this.data.big != 1){
      this.data.big = this.data.big-1
    }
    if(this.data.ii > 0){
      this.data.ii = this.data.ii - 1
    }
    this.setData({
      nums:arr,
      big: this.data.big,
      ii: this.data.ii,
      danArr: this.data.danArr
    })
  },
//添加选项
  addQ:function(){
    this.data.ii++
    this.data.big++
    console.log("添加选项",this.data.ii)
    this.data.danArr[this.data.big] = ""
    this.data.num[this.data.ii] = this.data.big;
    this.setData({
      nums: this.data.num,
      ii: this.data.ii,
      danArr: this.data.danArr
    })
  },
  //判断答题完成情况
  formSubmit: function () {
    var finish;
    var i = 0;
    var _this = this;
    while (i < len) {
      if (qnaire.qnaire[i].ifMust == 0) {
        finish = 'true';
      }
      //Object.keys(ans[i]).length === 0 用来判断对象长度的
      else if (ans[i] == "") {
        finish = 'false';
        break;
      } else {
        finish = 'true';
      }
      i++;
    }
    if (finish == 'false') {
      wx.showModal({
        title: '无法提交',
        content: '您还有部分题目未完成，请检查后重新提交',
        showCancel: false,
        confirmColor: '#fcbe39',
        confirmText: "好的",
        success(res) {
          _this.setData({
            id: i,
          })
        }
      })
    } else {
      wx.showLoading({
        title: '加载中',
      })
      wx.request({
        url: api.Question_UPDATE_URL,
        method: 'POST',
        dataType: 'json',
        data: {
          answer: ans
        },
        header: { 'content-type': 'application/json' },
        success: function (res) {
          wx.hideLoading()
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        },
        fail: function (res) {
          wx.showToast({
            title: '提交失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  },

  //将用户完成的答案数组上传至云数据库
  answer2db: function () {
    for (var i = 0; i < ans.length; i++) {
      if (!ans[i]) {
        ans[i] = ""
      }
    }
    console.log("提交", ans)


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    nums[0] = 1;
    this.setData({
      nums : nums
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})